package com.molichuxing.services.infrastructure.service.impl;

import com.alibaba.fastjson.JSON;
import com.molichuxing.services.business.dto.request.create.GoodsSubscribeCreateDto;
import com.molichuxing.services.infrastructure.bean.GoodsSubscribeBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.GoodsSubscribeMapper;
import com.molichuxing.services.infrastructure.dto.response.GoodsSubscribeDto;
import com.molichuxing.services.infrastructure.service.GoodsSubscribeService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 订阅里程商品表 服务实现类
 * </p>
 *
 * @author xuelin yi
 * @since 2019-08-12
 */
@Service("goodsProductSubscribeService")
public class GoodsSubscribeServiceImpl implements GoodsSubscribeService {

    private static final Logger log = Logger.getLogger(GoodsSubscribeServiceImpl.class);

    @Resource
    private GoodsSubscribeMapper goodsSubscribeMapper;

    @Override
    public Map<Integer, GoodsSubscribeDto> getGoodsSubscribeById(List<Integer> goodsId) {
        log.info("根据商品id集合查询订阅商品信息。入参 goodsId ： ["+ goodsId +"]");
        List<GoodsSubscribeBean> result = goodsSubscribeMapper.getBatchIds(goodsId);
        log.info("根据商品id集合查询订阅商品信息 出参：queryWrapper： "+ JSON.toJSONString(result));
        return Convert.toGoodsProductSubscribeDto(result).stream().collect(Collectors.toMap(GoodsSubscribeDto::getId, a -> a,(k1, k2)->k1));
    }

    @Override
    public GoodsSubscribeDto getGoodsSubscribeById(Integer goodsId) {
        log.info("根据商品id查询订阅商品信息。入参 goodsId ： ["+ goodsId +"]");
        GoodsSubscribeBean result = goodsSubscribeMapper.getById(goodsId);
        log.info("根据商品id集合订阅商品信息 出参：queryWrapper： "+ JSON.toJSONString(result));
        return Convert.toGoodsProductSubscribeDto(result);
    }

    @Override
    public int createSubscribeGoodsProduct(GoodsSubscribeCreateDto createDto) {
        log.info("新增订阅商品信息列表。 入参 createDto ： ["+ createDto +"]");
        GoodsSubscribeBean goodsProductExclusiveBean = Convert.toSubscribeGoodsProductBean(createDto);
        int result = goodsSubscribeMapper.create(goodsProductExclusiveBean);
        log.info("新增订阅商品信息列表。 出参 result ： ["+ result +"]");
        return goodsProductExclusiveBean.getId();
    }
}
